package controllers;

import java.util.List;

import models.Member;
import play.cache.Cache;
import play.data.validation.Error;
import play.data.validation.Validation.ValidationResult;
import play.db.jpa.GenericModel.JPAQuery;
import play.mvc.*;
import play.mvc.Scope.RenderArgs;

public class Login extends Controller {

	public static void index() {
		render();
	}

	public static void login(String UEmail, String UPwd, String rememberme) {

		validation.required(UEmail).key("UEmail").message("请输入邮箱");
		validation.required(UPwd).key("UPwd").message("请输入密码");

		Member member = Member.find("byLoginemail", UEmail).first();

		if (null != member && member.password.equals( UPwd)) {

			session.put("memberid", member.id);
			
			session.put("memberemail", member.loginemail);
			Application.index();

		} else {

			if (null == member) {
				validation.addError("UEmail", "无此邮箱", "");
			} else {
				validation.addError("UPwd", "密码错误", "");
			}
			if (validation.hasErrors()) {

				params.flash();
				validation.keep();
			}

			Login.index();
		}

	}

	public static void logout(String UEmail, String UPwd, String rememberme) {

		session.remove("memberid");

		Application.index();
	}
}
